/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 * The cell of the table can be colored. For this version, only implemented the function that grey out records.
 * - tooltip on each cell as long as it contain String type of data
 * @author: jin Chen
 * @date: Aug 16, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits.table;



import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.HashSet;
import java.util.Vector;

public class ColorConfigureTable extends ConfigureTable {


    public ColorConfigureTable() {


        useCustomizedRender();
        TableModel model=getAModel();
        setModel(model);
    }
    //need overrided by subclass
    protected  void useCustomizedRender(){
         TableCellRenderer render=new  GreyCellRender();
        //Class columnClass = this.getColumnClass(0);   //String.  Column class is the data type, it can be String, Integer.....
         // 
        this.setDefaultRenderer(String.class,render);
        //this.setDefaultRenderer(Integer.class,render);  
    }

    public TableCellRenderer getDefaultCellRender(){
         return this.getDefaultRenderer(String.class );
    }

    /*******************************************************************************************************
     *                service
     *******************************************************************************************************/

        public void colorItem(int index,boolean gray) {
            GreyCellRender render=(GreyCellRender) this.getDefaultCellRender() ;
            render.colorItem(index,gray);
            this.repaint() ;
        }
        public void colorAllItem(boolean gray) {
            GreyCellRender render=(GreyCellRender) this.getDefaultCellRender() ;
            for (int i=0;i<this.getModel().getRowCount() ;i++){
                render.colorItem(i,gray);
            }
            this.repaint() ;
        }
      public JToolTip createToolTip(){
        JToolTip toolTip = super.createToolTip();
            //toolTip.setForeground(Color.red );
        toolTip.setBackground(new Color(252,253,205) );
        /*toolTip.setFont(new Font("TimesRoman",
                             Font.ITALIC , 16));
*/
           return toolTip;
    }

    //temp
    /*public  void addRecord(String name) {
        Object[] arow={name, "teswt"};
                addRow(arow);
        //Object[] arow={columnName, new Integer(rowCount) };

    }*/
    class GreyCellRender extends DefaultTableCellRenderer{
        Color specialForeground=Color.gray;
        HashSet rowsOfGray=new HashSet();
        /**
         *
          * @param index
         *  @return  true if the row is required to be in specialForeground color
         */
        public boolean isRowColored(int index){
            return this.rowsOfGray.contains(new Integer(index));
        }

        public void colorItem(int index,boolean grey) {
            ///System.out.println("colorItem():"+index);
            if(grey)
                this.rowsOfGray.add(new Integer(index));
            else
                this.rowsOfGray.remove(new Integer(index));
        }

    // implements javax.swing.table.TableCellRenderer
    /**
     *
     * Returns the default table cell renderer.
     *
     * @param table  the <code>JTable</code>
     * @param value  the value to assign to the cell at
     *			<code>[row, column]</code>
     * @param isSelected true if cell is selected
     * @param hasFocus true if cell has focus
     * @param row  the row of the cell to render
     * @param column the column of the cell to render
     * @return the default table cell renderer
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
                          boolean isSelected, boolean hasFocus, int row, int column) {
        //System.out.println("cell["+row+","+column+"]");
        JComponent comp=(JComponent) super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
        Object v = ColorConfigureTable.this.getValueAt(row,column);
        // 
        if(v instanceof String ){
            comp.setToolTipText((String) v);
            //comp.setBackground(Color.)

        }
        //System.out.println("row:"+row);
        if(this.isRowColored(row) ){// 
            super.setForeground(this.specialForeground);
        }
        else{
            super.setForeground(table.getForeground());  //set as original color
            //super.setForeground(this.);
        }
        return comp;


    } //method

        public JToolTip createToolTip() {
            return super.createToolTip();    //To change body of overridden methods use File | Settings | File Templates.
        }
    }//inner class

     public  static TableModel getAModel(){
        Vector colName=new Vector();
        colName.add("Name");colName.add("NumRec");
        Class[] types = new Class [] {
                 String.class, String.class
        };

        String [] rec1={"row1","des1"};
        String [] rec2={"row2","des2"};
        String [] rec3={"row3","des3"};
        Object[][] data=new Object[][]{rec1,rec2,rec3};

        String [] colnames=new String[]{"Name","NumRec"};


        boolean[] editable={false,false,false};
        ConfigureTableModel model=new ConfigureTableModel(data,colnames);
        model.setTypes(types);
        model.setEditables(editable);
        return model;
    }



}
